package com.sq.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sq.common.utils.StringUtils;
import com.sq.common.web.R;
import com.sq.common.web.base.TableDataInfo;
import com.sq.common.web.domain.AjaxResult;
import com.sq.system.base.BaseController;
import com.sq.system.pojo.SysMenu;
import com.sq.system.pojo.SysRole;
import com.sq.system.pojo.SysRoleMenu;
import com.sq.system.service.SysMenuService;
import com.sq.system.service.SysRoleMenuService;
import com.sq.system.service.SysRoleService;
import com.sq.system.utils.UserInfoUtil;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Author Li sir
 * @Date 2024/9/30
 */

@RestController
@RequestMapping("/api/role/")
public class SysRoleController extends BaseController {

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    @Autowired
    private SysMenuService sysMenuService;

    @PostMapping("insertRole")
    public R<Integer> insertRole(@RequestBody SysRole sysRole){
        int i = sysRoleService.insertRole(sysRole);
        return R.ok(i);
    }


    @PostMapping("select")
    public TableDataInfo selectWhere(@RequestBody SysRole sysRole){
        startPage(sysRole.getPageNum(),sysRole.getPageSize());
        List<SysRole> sysRoleList = sysRoleService.selectRoleWhere(sysRole);
        return getDataTable(sysRoleList);
    }

    @PostMapping("insertOrUpdate")
    public AjaxResult insertOrUpdate(@RequestBody SysRole sysRole){
        boolean off = false;
        if (StringUtils.isBlank(sysRole.getRoleId())){
            //新增
            int i = sysRoleService.insertRole(sysRole);
            if (i > 0){
                off = true;
            }
        }else {
            // 更新
           off =  sysRoleService.updatRole(sysRole);
        }

        if (off){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @PostMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody SysRole role)
    {
//        roleService.checkRoleAllowed(role);
//        roleService.checkRoleDataScope(role.getRoleId());
        role.setUpdateBy(UserInfoUtil.getUserName());
        role.setUpdateTime(new Date());
        return toAjax(sysRoleService.updateRoleStatus(role));
    }

    @GetMapping("getone/{roleId}")
    public AjaxResult getOne(@PathVariable("roleId") String roleId){
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",roleId);
        SysRole one = sysRoleService.getOne(queryWrapper);
        return AjaxResult.success(one);

    }

    @GetMapping("delete/{roleId}")
    public AjaxResult delete(@PathVariable("roleId") String roleId){
        boolean b = sysRoleService.removeById(roleId);
        if (b){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @GetMapping("/getrolemenu/{roleId}")
    public AjaxResult getrolemenu(@PathVariable("roleId") String roleId){
        // 查询所有的菜单树
        List<SysMenu> sysRoleMenu = sysMenuService.selectAllMenuTree();

        // 查询当前角色已选择的菜单id
        List<Long> roleMenuIds = sysRoleMenuService.selectRoleMenuId(roleId);

        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put("allmenu",sysRoleMenu);
        ajaxResult.put("rolemenu",roleMenuIds);

        return ajaxResult;
    }

    @PostMapping("dataScope")
    public AjaxResult dataScope(@RequestBody dataSpoce dataSpoce){
        boolean b = sysRoleMenuService.dataScopeUpdate(dataSpoce.roleId, dataSpoce.getMenuids());
        if (b){
            return success();
        }
        return error();
    }

    @Data
    public static class dataSpoce{
        private String roleId;
        private List<Integer> menuids;
    }

}
